﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PluginInterface;
using System.Windows.Forms;
using System.Drawing;
using Chatclient;
using System.Security;
using System.Security.Permissions;
using System.Runtime.InteropServices;

namespace SmileyPack
{
    public class SmileyFunctionality : IPlugin
    {
        IPluginHost myPluginHost = null;
        string myPluginName = "ExtendedPack";
        string myPluginAuthor = "MessengerTeam";
        string myPluginDescription = "Fügt die Funktionalität der Smiley-Anzeige hinzu.";
        string myPluginVersion = "1.0.0";

        #region IPlugin
        public IPluginHost Host
        {
            get
            {
                return myPluginHost;
            }
            set
            {
                myPluginHost = value;
            }
        }

        public string Name
        {
            get { return myPluginName; }
        }

        public string Description
        {
            get { return myPluginDescription; }
        }

        public string Author
        {
            get { return myPluginAuthor; }
        }

        public string Version
        {
            get { return myPluginVersion; }
        }

        public void Initialize()
        {
           // 
        }

        public void Dispose()
        {
           // 
        }

        #endregion

        #region Work
        public void handleMessage(MessageHandler.Message message, object form)
        {
           
        }

        Dictionary<string, Image> emoticons = new Dictionary<string, Image>();

        public void addFunctionality(System.Windows.Forms.Form form)
        {
            FormPrivateChat fpc = (FormPrivateChat)form;
            RichTextBox chatBox = fpc.txtBxChat;
            chatBox.TextChanged += new EventHandler(chatBox_TextChanged);

            // Smileys
            try
            {

                emoticons.Add(":-)", (Image)SmileyPack.SmileyRes.smiley_smiling);
                emoticons.Add(":)", (Image)SmileyPack.SmileyRes.smiley_smiling);
                emoticons.Add(":-D", (Image)SmileyPack.SmileyRes.smiley_grinning);
                emoticons.Add(":D", (Image)SmileyPack.SmileyRes.smiley_grinning);
                emoticons.Add(":-(", (Image)SmileyPack.SmileyRes.smiley_sad);
                emoticons.Add(":(", (Image)SmileyPack.SmileyRes.smiley_sad);
                emoticons.Add(";-)", (Image)SmileyPack.SmileyRes.smiley_winking);
                emoticons.Add(";)", (Image)SmileyPack.SmileyRes.smiley_winking);
            }
            catch
            {
            }

        }

        private void chatBox_TextChanged(object sender, EventArgs e)
        {
            try
            {

                UIPermission uip = new UIPermission(UIPermissionWindow.AllWindows, UIPermissionClipboard.AllClipboard);
                uip.Demand();
            }
            catch (SecurityException se)
            {
                MessageBox.Show("UI Permission Refused");
            }
            RichTextBox txtBxChat = (RichTextBox)sender;

            foreach (string emote in emoticons.Keys)
            {
                while (txtBxChat.Text.Contains(emote))
                {

                    int ind = txtBxChat.Text.IndexOf(emote);
                    txtBxChat.Select(ind, emote.Length);

                    try
                    {
                        Clipboard.SetImage(emoticons[emote]);
                        txtBxChat.ReadOnly = false;
                        txtBxChat.Paste();
                        txtBxChat.ReadOnly = true;
                        Clipboard.Clear();
                    }
                    catch (Exception ex)
                    {
                        string msg = ex.Message;
                        msg += Environment.NewLine;
                        msg += Environment.NewLine;
                        MessageBox.Show(msg);
                    }                
                }
            }
        }
   
        #endregion

    }
}
